<?php
/**
 * *
 *  * ============================================================================
 *  * Created by PhpStorm.
 *  * User: Ice
 *  * 邮箱: ice@sbing.vip
 *  * 网址: https://sbing.vip
 *  * Date: 2019/9/19 下午3:33
 *  * ============================================================================.
 */

namespace app\admin\controller;

use think\facade\Config;
use app\common\controller\Backend;
use app\admin\model\user\User;
use app\admin\model\user\Order;
use think\facade\Session;
/**
 * 控制台.
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    /**
     * 查看.
     */
    public function index()
    {
//        $seventtime = \fast\Date::unixtime('day', -7);
//        $paylist = $createlist = [];
//        for ($i = 0; $i < 7; $i++) {
//            $day = date('Y-m-d', $seventtime + ($i * 86400));
//            $createlist[$day] = mt_rand(20, 200);
//            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
//        }
//        $hooks = config('addons.hooks');
//        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',',
//            $hooks['upload_config_init']) : 'local';
//        $config = Config::get('composer');
//        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $user_model=new User();
        $order_model=new Order();
        $agent_id=Session::get('admin.agent_id');
        if(!empty($agent_id)){
            $user_count=$user_model->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->count();
            $user_today=$user_model->whereTime('create_time','today')->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->count();
            $user_yesterday=$user_model->whereTime('create_time','yesterday')->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->count();
            $order_user=$order_model->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->where('status',1)->group('user_id')->count();
            
            $order_count=$order_model->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->where('status',1)->count();
            $order_sum=$order_model->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->where('status',1)->sum('price');
            $order_today=$order_model->whereTime('create_time','today')->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->where('status',1)->sum('price');
            $order_ali=$order_model->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->where('status',1)->where('pay_type',1)->sum('price');
            $order_wx=$order_model->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->where('status',1)->where('pay_type',2)->sum('price');
            $order_yesterday=$order_model->whereTime('create_time','yesterday')->where('agent_id',$agent_id)->whereOr('p_agent_id',$agent_id)->where('status',1)->sum('price');
        }else{
            $user_count=$user_model->count();
            $user_today=$user_model->whereTime('create_time','today')->count();
            $user_yesterday=$user_model->whereTime('create_time','yesterday')->count();
            $order_user=$order_model->where('status',1)->group('user_id')->count();

            $order_count=$order_model->where('status',1)->count();
            $order_sum=$order_model->where('status',1)->sum('price');
            $order_today=$order_model->whereTime('create_time','today')->where('status',1)->sum('price');
            $order_ali=$order_model->where('status',1)->where('pay_type',1)->sum('price');
            $order_wx=$order_model->where('status',1)->where('pay_type',2)->sum('price');
            $order_yesterday=$order_model->whereTime('create_time','yesterday')->where('status',1)->sum('price');
        }
        $this->view->assign([
            'totaluser'        => $user_count,
            'user_today'        => $user_today,
            'user_yesterday'        => $user_yesterday,
            'order_user'        => $order_user,
            'order_num'       => $order_count,
            'order_price'       => $order_sum,
            'order_ali'       => $order_ali,
            'order_wx'       => $order_wx,
            'order_today'       => $order_today,
            'order_yesterday'       => $order_yesterday,
        ]);

        return $this->view->fetch();
    }
}
